<?php

/**
 * Auth
 *
 * @package Controllers
 * @author  Duy Trung <trungnguyen@perselab.com>
 * @since   Version 1.0
 */
// Models
require_once './models/User.php';
require_once './models/Recipe.php';
require_once './models/Ingredient.php';
require_once './models/Step.php';
require_once './models/Image.php';

class Auth
{
	public function login($app,$username = '',$password = '')
	{
		$user = Model::factory('User')->where_equal('name', $username)->find_one();
		
		if($user instanceof User){
			if($user->password == $password){
				$recipes = $user->recipes()->find_many();
				$recipeMap = array();
				$ingredientMap = array();
				foreach ($recipes as $recipe) {
					//ingredients
					$ingredients = $recipe->ingredients()->find_many();
					foreach ($ingredients as $ingredient) {
						$ingredientImages = $ingredient->images()->find_many();
						$ingredientMap[$ingredient->id] = $ingredientImages;
					}
					//steps
					$steps = $recipe->steps()->find_many();
					//images
					$images = $recipe->images()->find_many();
					$categories = $recipe->categories()->find_many();
					//todo generate xml not completed more work on this. still need to apply
					//images, ingredients, steps
					$recipeMap[$recipe->id] = array('ingredients' => $ingredients, 'steps' => $steps, 'images' => $images, 'categories' => $categories);
				}
				
				return $app->render('login.xml', array(
					'user' => $user,
					'recipes' => $recipes,
				 	'recipeMap' => $recipeMap,
				 	'ingredientMap' => $ingredientMap
				), null, 'text/xml');
			}else{
				return $app->render('login.xml', array('user' => null), null, 'text/xml');
			}
		}else{
			return $app->render('login.xml', array('user' => null), null, 'text/xml');
		}
		
	}
	
	public function register($app, $username, $password, $email){
		$count_username = Model::factory('User')->where_equal('name', $username)->count();

		if( $count_username == 0){
			$count_email = Model::factory('User')->where_equal('email', $email)->count();
			if($count_email == 0){
				$user_create = Model::factory('User')->create();
				$user_create->name = $username;
				$user_create->password = $password;
				$user_create->email = $email;
				$user_create->fullname = $username;
				$user_create->image_id = null;
				$user_create->salt = substr(md5(time()), 0, 4);
				$user_create->sex = 1;
				$user_create->create_at = date('Y-m-d H:i:s');
				$user_create->update_at = date('Y-m-d H:i:s');
				$user_create->save();
				
				return $app->render('register.xml', array('user' => $user_create,'status'=>1,'message'=>'success'), null, 'text/xml');
			}else{
				return $app->render('register.xml', array('user' => false,'status'=>-2,'message'=>'email already in use'), null, 'text/xml');
			}
		}else{
			return $app->render('register.xml', array('user' => false,'status'=>-1,'message'=>'username already in use'), null, 'text/xml');
		}
		
	}
	
	public function profile($app,$userid){
		$user = Model::factory('User')->where_equal('id', $userid)->find_one();
		
		if($user instanceof User){
			$recipes = $user->recipes()->find_many();
			$recipeMap = array();
			$ingredientMap = array();
			foreach ($recipes as $recipe) {
				//ingredients
				$ingredients = $recipe->ingredients()->find_many();
				foreach ($ingredients as $ingredient) {
					$ingredientImages = $ingredient->images()->find_many();
					$ingredientMap[$ingredient->id] = $ingredientImages;
				}
				//steps
				$steps = $recipe->steps()->find_many();
				//images
				$images = $recipe->images()->find_many();
				//todo generate xml not completed more work on this. still need to apply
				//images, ingredients, steps
				$recipeMap[$recipe->id] = array('ingredients' => $ingredients, 'steps' => $steps, 'images' => $images);
			}
			
			return $app->render('profile.xml', array(
				'user' => $user,
				'recipes' => $recipes,
			 	'recipeMap' => $recipeMap,
			 	'ingredientMap' => $ingredientMap
			), null, 'text/xml');

		}else{
			return $app->render('profile.xml', array('user' => null), null, 'text/xml');
		}
	}
}